package org.csu.laboratoryequipment.controller;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.CompKeyWord;
import org.csu.laboratoryequipment.entity.History;
import org.csu.laboratoryequipment.entity.Result;
import org.csu.laboratoryequipment.entity.Searchrecord;
import org.csu.laboratoryequipment.persistence.HistoryMapper;
import org.csu.laboratoryequipment.persistence.ResultMapper;
import org.csu.laboratoryequipment.service.HistoryService;
import org.csu.laboratoryequipment.service.ResultService;
import org.csu.laboratoryequipment.service.SearchrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private HistoryService historyService;

    @Autowired
    private SearchrecordService searchrecordService;

    @Autowired
    private ResultService resultService;



    @ResponseBody
    //获取该用户的历史搜索信息
    @GetMapping("getHistorySearchByUid/{uid}")
    public CommonResponse<List<History>> getHistorySearchByUid(@PathVariable("uid") String username){
        return historyService.getHistorySearchByUid(username);
    }

    @ResponseBody
    //获取所有历史搜索关键词
    @GetMapping("getAllKeyword")
    public CommonResponse<List<Searchrecord>> getAllKeyword(){
        return searchrecordService.getAllKeyword();
    }

    @ResponseBody
    //获取所有历史搜索关键词
    @PostMapping ("saveScore")
    public CommonResponse<String> saveScore(
            @RequestParam("userid") String userid,
            @RequestParam("history_word") String history_word,
            @RequestParam("score") double score,
            @RequestParam("com_avg") double com_avg,
            @RequestParam("time") String time
    ) throws ParseException {
        System.out.println("hhh");
        OffsetDateTime offsetDateTime = OffsetDateTime.parse(time);

        // 将 OffsetDateTime 转换为 Date
        Date date = Date.from(offsetDateTime.toInstant());
        return historyService.saveScore(userid,history_word,score,com_avg,date);
    }


    @ResponseBody
    @PostMapping("getResultByCompKey")
    public CommonResponse<List<Result>> getResultByCompKey(@RequestParam("uid") String uid,@RequestParam("compKey") String seedWord) throws Exception {
        List<Result> temp = new ArrayList<>();
        temp = resultService.searchByseedWord(seedWord);
        //先去result查找如果有
        if (!temp.isEmpty()) {
            return CommonResponse.createForSuccessMessage("SUCCESS", temp);
        }
        //result中没有，再用算法生成
        //result中的竞争关键词，竞争度
        List<CompKeyWord> result = new ArrayList<>();
        result = resultService.getCompKeyAndComNumBySeedWord(seedWord);
        //将结果插入result表
        resultService.addRecord(seedWord, result);
        return CommonResponse.createForSuccessMessage("SUCCESS", resultService.searchByseedWord(seedWord));
    }


        //    //    测试
        //    @Autowired
        //    private ResultMapper resultMapper;
        //    @Autowired
        //    private HistoryMapper historyMapper;
        //
        //
        //    @ResponseBody
        //    //获取所有历史搜索关键词
        //    @PostMapping("getResultByCompKey")
        //    public CommonResponse<List<Result>> getResultByCompKey(
        //            @RequestParam("uid") String uid,
        //            @RequestParam("compKey") String compKey
        //    ){
        //        return CommonResponse.createForSuccess(resultMapper.selectList(null));
        //    }


}
